package com.zx.deploy.common.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Class description
 *
 * @author V.E.
 * @version 1.0, 17/04/11
 */
public class DateUtil {

    /**
     * Field description
     */
    private static String defaultDatePattern = "yyyy-MM-dd";
    private static final TimeZone defult= TimeZone.getTimeZone("GMT+8");
    /**
     * 获得默认的 date pattern
     *
     * @return
     */
    public static String getDatePattern() {
        return defaultDatePattern;
    }

    /**
     * 返回预设Format的当前日期字符串
     *
     * @return
     */
    public static String getToday() {
        Date today = new Date();

        return format(today);
    }

    /**
     * 使用预设Format格式化Date成字符串
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return (date == null)
                ? " "
                : format(date, getDatePattern());
    }

    /**
     * 使用参数Format格式化Date成字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if(date == null){
        	return "";
        }else{
        	DateFormat df=new SimpleDateFormat(pattern);
    		df.setTimeZone(defult);
    		return df.format(date);
        }
    }

    /**
     * 使用预设格式将字符串转为Date
     *
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static Date parse(String strDate) throws ParseException {
        return StringUtils.isBlank(strDate)
                ? null
                : parse(strDate, getDatePattern());
    }

    /**
     * 使用参数Format将字符串转为Date
     *
     * @param strDate
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date parse(String strDate, String pattern) throws ParseException {
    	if(StringUtils.isBlank(strDate)){
    		return null;
    	}else{
    		DateFormat df=new SimpleDateFormat(pattern);
    		df.setTimeZone(defult);
    		return df.parse(strDate);
    	}
    }

    /**
     * 在日期上增加数个整月
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addMonth(Date date, int n) {
        Calendar cal = Calendar.getInstance(defult);

        cal.setTime(date);
        cal.add(Calendar.MONTH, n);

        return cal.getTime();
    }

    /**
     * Method description
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance(defult);

        // 年
        cal.set(Calendar.YEAR, Integer.parseInt(year));

        // 月，因为Calendar里的月是从0开始，所以要-1
        // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);

        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);

        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);

        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));    // 获得月末是几号
    }

    /**
     * Method description
     *
     * @param year
     * @param month
     * @param day
     * @return
     * @throws ParseException
     */
    public static Date getDate(String year, String month, String day) throws ParseException {
        String result = year + "- " + ((month.length() == 1)
                ? ("0 " + month)
                : month) + "- " + ((day.length() == 1)
                ? ("0 " + day)
                : day);

        return parse(result);
    }


/**
 * 获取本周第一天
* @author mazhongshan   
* @date 2018年2月1日 下午6:28:25
 */
public static String getWeekStartDateString(){
		Calendar cal = Calendar.getInstance(defult);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setTimeZone(defult);
		String firstDayOfWeek = sdf.format(cal.getTime());
		return firstDayOfWeek;
}

/**
 * 获取本周最后一天
 * @author mazhongshan   
 * @date 2018年2月1日 下午6:28:25
 */
public static String getWeekEndDateString(){
	Calendar cal = Calendar.getInstance(defult);
	cal.setFirstDayOfWeek(Calendar.MONDAY);
	cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6); // Sunday
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	sdf.setTimeZone(defult);
	String lastDayOfWeek = sdf.format(cal.getTime());
	return lastDayOfWeek;
}

/**
 * 获取某段时这里写代码片间内的所有日期
 * @param dBegin
 * @param dEnd
 * @return
 */
public static List<Date> findDates(Date dBegin, Date dEnd) {
    List<Date> lDate = new ArrayList<Date>();
    lDate.add(dBegin);
    Calendar calBegin = Calendar.getInstance(defult);
// 使用给定的 Date 设置此 Calendar 的时间
    calBegin.setTime(dBegin);
    Calendar calEnd = Calendar.getInstance(defult);
// 使用给定的 Date 设置此 Calendar 的时间
      calEnd.setTime(dEnd);
    // 测试此日期是否在指定日期之后
    while (dEnd.after(calBegin.getTime()))  {
     // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
         calBegin.add(Calendar.DAY_OF_MONTH, 1);
         lDate.add(calBegin.getTime());
    }
    return lDate;
}


/**
 * 获取本周第一天
* @author mazhongshan   
* @date 2018年2月1日 下午6:28:25
 */
public static Date getWeekStartDate(){
		Calendar cal = Calendar.getInstance(defult);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
		cal.set(Calendar.HOUR, 0);  
		cal.set(Calendar.MINUTE, 0);  
		cal.set(Calendar.SECOND, 0);  
		cal.set(Calendar.MILLISECOND, 0);  
		return cal.getTime();
}

/**
 * 获取本周最后一天
 * @author mazhongshan   
 * @date 2018年2月1日 下午6:28:25
 */
public static Date getWeekEndDate(){
	Calendar cal = Calendar.getInstance(defult);
	cal.setFirstDayOfWeek(Calendar.MONDAY);
	cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6); // Sunday
	cal.set(Calendar.HOUR, 23);  
	cal.set(Calendar.MINUTE, 59);  
	cal.set(Calendar.SECOND, 59);  
	cal.set(Calendar.MILLISECOND, 999);  
	return cal.getTime();
}




public static Date getToDayStartTime() {  
    Calendar todayStart = Calendar.getInstance(defult);  
    todayStart.set(Calendar.HOUR, 0);  
    todayStart.set(Calendar.MINUTE, 0);  
    todayStart.set(Calendar.SECOND, 0);  
    todayStart.set(Calendar.MILLISECOND, 0);  
    return todayStart.getTime();  
}  

public static Date getToDayEndTime() {  
    Calendar todayEnd = Calendar.getInstance(defult);  
    todayEnd.set(Calendar.HOUR, 23);  
    todayEnd.set(Calendar.MINUTE, 59);  
    todayEnd.set(Calendar.SECOND, 59);  
    todayEnd.set(Calendar.MILLISECOND, 999);  
    return todayEnd.getTime();  
}  

/**
 * 获取指定天的开始时间
* @author mazhongshan   
* @date 2018年2月10日 上午10:17:20
 */
public static Date getStartTimeByDayDate(Date date) {  
	Calendar todayStart = Calendar.getInstance(defult);  
	todayStart.setTime(date);
	todayStart.set(Calendar.HOUR, 0);  
	todayStart.set(Calendar.MINUTE, 0);  
	todayStart.set(Calendar.SECOND, 0);  
	todayStart.set(Calendar.MILLISECOND, 0);  
	return todayStart.getTime();  
}  

/**
 * 获取指定天的结束时间
* @author mazhongshan   
* @date 2018年2月10日 上午10:17:20
 */
public static Date getEndTimeByDayDate(Date date) {  
	Calendar todayEnd = Calendar.getInstance(defult);  
	todayEnd.setTime(date);
	todayEnd.set(Calendar.HOUR, 23);  
	todayEnd.set(Calendar.MINUTE, 59);  
	todayEnd.set(Calendar.SECOND, 59);  
	todayEnd.set(Calendar.MILLISECOND, 999);  
	return todayEnd.getTime();  
}  



}

//~ Formatted by Jindent --- http://www.jindent.com
